package com.upsuns.fileop;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import com.upsuns.multiop.ThreadBatch;
import com.upsuns.transfer.ClientTerminal;
import com.upsuns.utils.Constants;

/**
 *@Description:<p>文件读取线程</p>
 *@author 王旭
 *@time 2016-1-30 下午5:17:49
 */
public class FileReaderThread implements Runnable {
	
	private RandomAccessFile raf;
	
	private int flag; //线程标记
	
	private long start, len; //读取文件的范围
	
	private ClientTerminal client;
	
	
	public FileReaderThread(File file, long start, long len, int flag) {
		this.start = start;
		this.len = len;
		this.flag = flag;
		try {
			this.raf = new RandomAccessFile(file, "r");
		} catch (FileNotFoundException e) {
			System.out.println("文件读取线程" + flag + "创建随机读取文件失败");
			e.printStackTrace();
		}
		
		client = new ClientTerminal(Constants.ADDRESS, Constants.PORT_BASE + flag);
	}

	@Override
	public void run() {
		
		try {
			client.send(raf, start, len);
		} catch (IOException e) {
			System.out.println("文件读取线程" + flag + "发送数据失败");
			e.printStackTrace();
		}
		
		ThreadBatch.latch.countDown();
		
	}
	
	

}
